package com.mymall.serve.filter;

import com.alibaba.fastjson.JSON;
import com.mymall.serve.common.RootResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;


@Slf4j
public class ActuatorFilter implements Filter {

    private List<String> whiteIps;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("actuator过滤器 init");
        String whiteIpsParam = filterConfig.getInitParameter("whiteIps");
        List<String> strings = Arrays.asList(whiteIpsParam.split(","));
        if (CollectionUtils.isNotEmpty(strings)){
            whiteIps = strings;
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        log.info("actuator过滤器-dofilter");
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String requestURI = httpRequest.getRequestURI();
        log.info("访问url：{}", requestURI);
        if (!requestURI.startsWith("/actuator")){
            filterChain.doFilter(request, response);
            return;
        }
        String ipAddress = getIpAddress(httpRequest);
        log.info("访问shutdown的机器的原始IP：{}", ipAddress);
        log.info("IP白名单：{}", whiteIps);
        if (whiteIps.contains(ipAddress)) {
           /* response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.write("{\"code\":403,\"msg\":\"非法请求\",\"result\":\"\"}");
            writer.flush();
            writer.close();*/
            httpResponse.reset();
            httpResponse.setContentType("text/json;charset=UTF-8");
            httpResponse.getWriter().write(JSON.toJSONString(RootResult.error(403, "非法请求")));
            return;
        }

        filterChain.doFilter(request, response);
    }

    private String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
